package com.freshcity.page.welcome
{
	import com.freshcity.component.BaseGameObject;
	import com.freshcity.component.FLabel;
	import com.freshcity.core.BasePage;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.utils.StringUtil;
	import com.freshcity.welcome.AccountWin;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;

	import fl.controls.ComboBox;
	import fl.controls.RadioButtonGroup;

	import flash.events.MouseEvent;

	public class EmailRegister extends BasePage
	{
		private var isLogin:Boolean;
		public var mainGroup:AccountWin;
		public var loginData:*;

		override public function initialize():void
		{
			super.initialize();

			mainGroup=new AccountWin();
			
			mainGroup.femaleRadio.visible=false;
			mainGroup.emailOne.visible=false;
			mainGroup.emailOneLabel.visible=false;
			mainGroup.passwordOne.visible=false;
			mainGroup.passwordOneLabel.visible=false;
			mainGroup.assoBtn.visible=false;
			
			layer.sceneLayer.centerPopup(mainGroup);
			rootClass.addChild(mainGroup);

			mainGroup.createBtn.addEventListener(MouseEvent.CLICK, onCreateAction);
			mainGroup.assoBtn.addEventListener(MouseEvent.CLICK, onAssoAction);
		}

		protected function onAssoAction(event:MouseEvent):void
		{
			var data:Object={};
			data.action="asso";
			data.src=loginData.src;
			data.account=loginData.account;
			data.id=StringUtil.trim(mainGroup.emailOne.text);
			data.password=StringUtil.trim(mainGroup.passwordOne.text);
			if (valid(data))
			{
				mainGroup.assoBtn.enabled = false;
				commu.send(OpCodes.REGISTER_THIRD_PART, data, onThirdPartRegister);
			}
		}

		protected function onCreateAction(event:MouseEvent):void
		{
			loginData.action="new";
			loginData.id=StringUtil.trim(mainGroup.emailTwo.text);
			loginData.password=StringUtil.trim(mainGroup.passwordTwo.text);
			if (valid(loginData))
			{
				mainGroup.createBtn.enabled = false;
				commu.send(OpCodes.REGISTER_THIRD_PART, loginData, onThirdPartRegister);
			}
		}

		private function valid(data:*):Boolean
		{
			if (data.id == "")
			{
				msg.showRollTipsMsg("邮箱未填写");
				return false;
			}
			if (!StringUtil.isEmail(data.id))
			{
				msg.showRollTipsMsg("无效的邮箱格式");
				return false;
			}
			if (data.password == "")
			{
				msg.showRollTipsMsg("密码未填写");
				return false;
			}
			if (data.password.length < 6 || data.password.length > 14)
			{
				msg.showRollTipsMsg("密码长度6~14位，字母区分大小写");
				return false;
			}
			return true;
		}

		private function onThirdPartRegister(data:Object):void
		{
			if (data is String)
			{
				msg.showRollTipsMsg(String(data));
				mainGroup.assoBtn.enabled = true;
				mainGroup.createBtn.enabled = true;
			}
			else if (data.action == 'new')
			{
				TweenLite.to(this.mainGroup, 0.5, {x: "-800"});
				var dataInit:DataInit=new DataInit();
				dataInit.owningGroup=this.owningGroup;
				dataInit.loginUser=data;
				dataInit.initialize();
				TweenLite.from(dataInit.back, 0.3, {onComplete: hide, alpha: 0, delay: 0.4});
			}
			else
			{
				commu.send(OpCodes.ENTRY, data, onLoad);
			}
		}

		private function onLoad(data:*):void
		{
			TweenLite.to(this.mainGroup, 0.5, {onComplete: destroy, x: "-800"});
			var main:MainScene=new MainScene();
			main.owningGroup=this.owningGroup;
			main.user=data.user;
			main.cond = data.cond;
			GlobalVar.LOGIN_USER=main.user;
			main.initialize();
		}

		public function hide():void
		{
			mainGroup.createBtn.removeEventListener(MouseEvent.CLICK, onCreateAction);
			mainGroup.assoBtn.removeEventListener(MouseEvent.CLICK, onAssoAction);
			this.destroy();
		}
	}
}
